package java6681.more;

import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.geometry.HPos;
import javafx.geometry.VPos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.io.PrintStream;
import java.net.URL;
import java.time.LocalDate;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;
import java.util.Arrays;

/**
 * @author Junichiro
 * @description 显示日历
 * @date 2021-03-23 21:36
 **/

/*
 * 分析：
 *  1. 获取指定月份的天数(最后一天) -> 遍历显示
 *  2. 按格则显示日历（月历最多跨六周，有6行7列，共42个格子）
 * */
public class Calendar6681Controller {
    private int currentYear;//当前显示月历的年
    private int currentMoth;//当前显示月历的月

    @FXML
    private DatePicker dpSelect;

    @FXML
    private Label lblMonth;

    @FXML
    private GridPane gpDayList;

    /*月历的每个格子中添加一个Label，用于显示数字...*/
    @FXML
    private void initialize() {
        /*月历最多6行7列*/
        int max = 7;
        for (int iRow = 1; iRow < max; iRow++) {
            /*月历按行显示，所以按行添加*/
            for (int iCol = 0; iCol < max; iCol++) {
                Label label = new Label();
                GridPane.setValignment(label, VPos.CENTER);
                GridPane.setHalignment(label, HPos.CENTER);
                gpDayList.add(label, iCol, iRow);
            }
        }
        dpSelect.setValue(LocalDate.now());
        onClick();
    }

    @FXML
    void onClick() {
        clear();
        LocalDate value = dpSelect.getValue();
        String s = String.format("%tY年%tB", value, value);
        if (dpSelect.getValue() == null) {
            /*如果没有选择日期，默认为当天的日期*/
            dpSelect.setValue(LocalDate.now());
        }
        int newYear = dpSelect.getValue().getYear();
        int newMonth = dpSelect.getValue().getMonthValue();

        if (currentYear == newYear && currentMoth == newMonth) {
            /*如果年，月没有变化，不重新显示日历*/
        } else {
            currentYear = newYear;
            currentMoth = newMonth;
            /*填充月历*/
            fill(newYear, newMonth);
            lblMonth.setText(s + "的日历");
        }
    }
    private void clear(){
        for (int i = 8; i < 50; i++) {
            Label lable = (Label)gpDayList.getChildren().get(i);
            lable.setText("");
            lblMonth.setText("");
        }
    }

    private void fill(int year, int month) {
        ObservableList<Node> list = gpDayList.getChildren();//GridPane的所有子节点
        int gpStartIndex = 8;//GridPane显示日历的起点元素序号是从8开始，分割线也是一个元素！！

        /*(1)清空本月的第一天之前的格子*/
        YearMonth yearMonth = YearMonth.of(year, month);
        LocalDate start = yearMonth.atDay(1);//本月的第一天
        /*本月第一天的星期对应的数字=需要清空的数量*/
        /*the day-of-week,from 1 (Monday) to 7 (Sunday)*/
        int weekStart = start.getDayOfWeek().getValue();

        /*本月第一天之前的格子内容清空*/
        int i = 0;
        for (; i < weekStart; i++) {
            ((Label) list.get(i + gpStartIndex)).setText("");
        }
        /*(2)填写本月的日期*/
        int nDays = yearMonth.lengthOfMonth();//本月的天数
        int day = 1;
        for (; i < weekStart + nDays; i++) {
            ((Label) list.get(i + gpStartIndex)).setText("" + day++);
        }
        /*(3)后面的格子内容清空*/
        for (; i < list.size() - gpStartIndex; i++) {
            ((Label) list.get(i + gpStartIndex)).setText("");
        }
    }

}
//        /*清空*/
//        if (value == null) {
//            lblMonth.setText(" ");
//            gpDayList.getChildren().clear();
//            return;
//        }
//
//        /*封装日期*/
//        LocalDate localDate = LocalDate.of(2021, 3, 1);
//        /*获取需要的日期*/
//        localDate = localDate.with(dpSelect.getValue());
//        /*获取某月的最后一天*/
//        TemporalAdjuster day = TemporalAdjusters.lastDayOfMonth();
//        localDate = localDate.with(day);
//        int end = localDate.getDayOfMonth();
//        /*遍历输出*/
//        for (int j = 0; j < end; j++) {
//            label = new Label(j + 1 + "");
//            int i = j + 1;
//            gpDayList.add(label, i % 7, 2 + i / 7);
//            GridPane.setHalignment(label, HPos.CENTER);
//        }